package utils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

/**
 * 吉利笔试题：
 * test方法没有全部通过
 */
public class Jili {
    public static void main(String[] args) {
        Jili jili = new Jili();
//        for (int i = 1; i < 1000; i++){
//            System.out.println(i + " "  + jili.convertToTitle(i));
//        }
        System.out.println(jili.convertToTitle(677));

    }

    public double test(int[] nums1, int[] nums2){
        int sumlength = nums1.length + nums2.length;

        int middleIndex = sumlength / 2;
        int currentIndex = 0;
        int n1Index = 0;
        int n2Index = 0;
        int currentNum = Math.min(nums1[0], nums2[0]);
        int current1 = 0;
        int current2 = 0;
        int max = Math.max(nums1[nums1.length- 1], nums2[nums2.length - 1]);
        while (currentIndex <= middleIndex){
            if (n1Index < nums1.length && nums1[n1Index] >= currentNum){
                current1 = nums1[n1Index];
            }else{
                current1 = max;
            }

            if (n2Index < nums2.length && nums2[n2Index] >= currentNum){
                current2 = nums2[n2Index];
            }else{
                current2 = max;
            }

            if (current1 < current2){
                n1Index++;
                currentIndex++;
                currentNum = current1;
            }else {
                n2Index++;
                currentIndex++;
                currentNum = current2;
            }


        }

        return currentNum;
    }

    public int maxLength(String s){
        List<Character> window = new ArrayList<>();
        char[] arr = s.toCharArray();

        int maxLength = 0;
        for (int i = 0; i < arr.length; i++){
            if (window.contains(arr[i])){
                int index = window.indexOf(arr[i]);
                window.add(arr[i]);
                window = window.subList(index + 1, window.size());

            }else{
                window.add(arr[i]);

            }

//            for (Character w:window){
//                System.out.print(w);
//            }
//            System.out.println();

            maxLength = Math.max(maxLength, window.size());
        }

        return maxLength;

    }

    public String convertToTitle(int  columnNumber) {
        if (columnNumber >= 1){
            columnNumber--;
        }
        //1.将A-Z转换为数组
        char[] chars = new char[26];
        char aChar = 'A';
        for (char index = 0; index < 26; index++){
            chars[index] = (char) (aChar + index);
        }

        //2.不断对columnNumber进行除法操作
        int result = columnNumber /26;
        int mod = columnNumber % 26;
        StringBuffer stringBuffer = new StringBuffer();

        List<Integer> data = new ArrayList();
        data.add(mod);
        do{
            if (data.size() > 0  && result > 0){
                result--;
            }
            System.out.print(mod + " ");
            data.add(mod);
            mod = result % 26;
            result = result /26;

        }while (result != 0 || mod != 0);

        Collections.reverse(data);

        for(int i : data){
            stringBuffer.append(chars[i]);
        }


        return stringBuffer.toString();
    }

}
